﻿using System;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Public
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HYPipeShow : HYNoTranExternalCommand
	{
		public override Result HYExecute()
		{
			Transaction transaction = null;
			Result result;
			try
			{
				PipeShowHelper pipeShowHelper = new PipeShowHelper();
				transaction = new Transaction(base.Doc, "管道显示");
				transaction.Start();
				string text = Path.Combine(Product.UserDataLocation, "HYPipeShowSet.xml");
				if (!File.Exists(text))
				{
					File.Copy(Path.Combine(Product.DataLocation, "User", "HYPipeShowSet.xml"), text);
				}
				for (;;)
				{
					PipeShowSetForm pipeShowSetForm = new PipeShowSetForm(text, base.Doc);
					pipeShowSetForm.ShowDialog();
					if (pipeShowSetForm.DialogResult == DialogResult.Abort)
					{
						pipeShowHelper.ShowPipe(text, base.Doc, pipeShowSetForm.isElectric, pipeShowSetForm.dDiameter);
						base.Doc.Regenerate();
						transaction.Commit();
						try
						{
							base.Sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)0, "查看状态，不能进行任何操作！");
							continue;
						}
						catch (Autodesk.Revit.Exceptions.OperationCanceledException)
						{
							return Autodesk.Revit.UI.Result.Failed;
						}
						break;
					}
					break;
				}
				transaction.Commit();
				result = Autodesk.Revit.UI.Result.Failed;
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.ToString());
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		public override void FilterProcess()
		{
			base.FilterViewType.Add(4);
			base.FilterProcess();
		}
	}
}
